package com.zjdiepu.www.rest.api.other;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.BusinessExtrasCodeType;
import com.zjdiepu.www.base.enums.type.BusinessExtrasType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.commpent.RedisObjectClient;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.ibiz.other.IOtherMessageAppService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessExtrasService;
import com.zjdiepu.www.model.other.dto.OtherMessageApp;
import com.zjdiepu.www.model.other.request.AuditDetailRequest;
import com.zjdiepu.www.model.other.response.OtherMessageAppResponseItem;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;
import com.zjdiepu.www.model.yw.response.ShenPiLiuZhuanResponse;

/**
 * APP消息 <br/>
 * @author xiongxiaotun<br/>
 * @time 2018年01月09日 11时24分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value = "AuditDetailController", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
@Api(tags = { "APP消息" }, produces = "application/json;charset=UTF-8")
public class AuditDetailController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private IOtherMessageAppService otherMessageAppService;
    @Resource
    private IYwBusinessExtrasService ywBusinessExtrasService;
    @Resource
    private TokenUserHelper tokenUserHelper;
    @Resource
	private RedisObjectClient redisObjectClient;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: auditDetail  <br/>
     * @Description: 获取风控结果详情 <br/>
     * @param otherSimpleInfo
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "获取风控结果详情", response = OtherMessageAppResponseItem.class, httpMethod="POST")
    @ApiImplicitParams({
		@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue = "ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue = "Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String") })
    @RequestMapping(value = "auditDetail", method = RequestMethod.POST)
    public ApiResponse auditDetail(@Validated(Add.class) @RequestBody AuditDetailRequest auditDetailRequest) {
    	tokenUserHelper.getUser();
    	String otherMessageAppId = auditDetailRequest.getOtherMessageAppId();
    	OtherMessageApp otherMessageApp = otherMessageAppService.selectById(otherMessageAppId);
    	 OtherMessageAppResponseItem mapOtherMessageApp = redisObjectClient.execute(new RedisCallback<OtherMessageAppResponseItem>() {
 			@Override
 			public OtherMessageAppResponseItem doInRedis(RedisConnection redisConnection)
 					throws DataAccessException {
 				OtherMessageAppResponseItem mapOtherMessageApp = mapOtherMessageApp(redisConnection, otherMessageApp);
 				
 				return mapOtherMessageApp;
 			}
        });
    	String busiId = otherMessageApp.getBusiId();
    	if(StringUtils.isBlank(busiId)) {
    		ShenPiLiuZhuanResponse shenPiLiuZhuanResponse = new ShenPiLiuZhuanResponse();
        	shenPiLiuZhuanResponse.setBusiId(busiId);
        	
    		YwBusinessExtras ywBusinessExtrasParam = new YwBusinessExtras();
    		ywBusinessExtrasParam.setBusiId(busiId);
    		ywBusinessExtrasParam.setCodeExtras(BusinessExtrasCodeType.ADD_GPS.getCode());
    		ywBusinessExtrasParam.setTypeExtras(BusinessExtrasType.AUDIT.getCode());
    		ywBusinessExtrasParam.setDpStatus(DpStatus.NORMAL.getCode());
    		
    		YwBusinessExtras gps = ywBusinessExtrasService.selectRefined(ywBusinessExtrasParam);
    		Integer gpsNum = 0;
    		if(null != gps) {
    			String valueExtras = gps.getValueExtras();
    			try {
    				gpsNum = StringUtils.isBlank(valueExtras) ? 0 : Integer.parseInt(valueExtras);
    			} catch(Exception e) {
    				logger.error("格式化GPS个数错误，valueExtras is {}.", valueExtras, e);
    			}
    		}
    		shenPiLiuZhuanResponse.setGps(gpsNum);
    		
    		ywBusinessExtrasParam.setCodeExtras(BusinessExtrasCodeType.ADD_GEPOSIT.getCode());
    		YwBusinessExtras yajin = ywBusinessExtrasService.selectRefined(ywBusinessExtrasParam);
    		Double yajinNum = 0.0;
    		if(null != yajin) {
    			String valueExtras = yajin.getValueExtras();
    			try {
    				yajinNum = StringUtils.isBlank(valueExtras) ? 0 : Double.parseDouble(valueExtras);
    			} catch(Exception e) {
    				logger.error("格式化征信押金错误，valueExtras is {}.", valueExtras, e);
    			}
    		}
    		shenPiLiuZhuanResponse.setCreditDeposit(yajinNum);
    		
    		ywBusinessExtrasParam.setCodeExtras(BusinessExtrasCodeType.FLEXIBLECHARGE.getCode());
    		YwBusinessExtras sfbt = ywBusinessExtrasService.selectRefined(ywBusinessExtrasParam);
    		Double sfbtNum = 0.0;
    		if(null != sfbt)
    		{
    			String valueExtras = sfbt.getValueExtras();
    			try {
    				sfbtNum = StringUtils.isBlank(valueExtras) ? 0 : Double.parseDouble(valueExtras);
    			} catch(Exception e) {
    				logger.error("格式化收费变通错误，valueExtras is {}.", valueExtras, e);
    			}
    		}
    		shenPiLiuZhuanResponse.setChargeChange(sfbtNum);
    		
    		ywBusinessExtrasParam.setCodeExtras(BusinessExtrasCodeType.IS_SPECIAL_ORDER.getCode());
    		YwBusinessExtras tsdd = ywBusinessExtrasService.selectRefined(ywBusinessExtrasParam);
    		boolean tsddB = false;
    		if(null != tsdd)
    		{
    			String valueExtras = tsdd.getValueExtras();
    			tsddB = "1".equals(valueExtras);
    		}
    		shenPiLiuZhuanResponse.setSpecialOrder(tsddB);
    		mapOtherMessageApp.setFjxx(shenPiLiuZhuanResponse);
    	}
        return ApiResponse.success(mapOtherMessageApp);
    }

}
